package ltd.smallwang.client.user.dto;

import cn.hutool.crypto.digest.DigestUtil;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import lombok.Data;

import java.util.regex.Pattern;

/**
 * @author justd
 */
@Data
public class UserRegisterCmd {
  private static final Pattern PASSWORD_CHECK =
      Pattern.compile("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\\d).{8,32}$");

  @NotBlank(message = "username can not empty")
  @Size(min = 1, max = 32, message = "username length must between 6 and 32")
  private String username;

  @NotEmpty(message = "password can not empty")
  @Size(min = 8, max = 32, message = "password length must between 8 and 32")
  private String password;

  @NotBlank(message = "email can not empty")
  @Email(message = "email format is incorrect")
  private String email;

  @NotEmpty(message = "verifyCode can not empty")
  @Size(min = 6, max = 6, message = "verifyCode length must be 6")
  private String emailVerifyCode;

  /**
   * 密码强度大
   *
   * @return the boolean
   */
  public boolean passwordIsStrong() {
    return PASSWORD_CHECK.matcher(password).find();
  }

  /**
   * Gets verify code cache key.
   *
   * @return the verify code cache key
   */
  public String getVerifyCodeCacheKey() {
    return "schedule_self_" + DigestUtil.sha256Hex(email) + "_" + emailVerifyCode;
  }
}
